﻿using System.Windows;
using System.Windows.Media;
using MTBrainStorm.Framework;

namespace MTBrainStorm.ViewModels
{
    public class JointViewModel : ObservableObject
    {
        #region Properties

        private MultiTouchElementViewModel _startElement;
        private MultiTouchElementViewModel _endElement;
        
        public MultiTouchElementViewModel StartElement
        {
            get { return _startElement; }
            set
            {
                if(_startElement != null)
                    _startElement.PropertyChanged -= Element1PropertyChanged;
                _startElement = value;
                _startElement.PropertyChanged += Element1PropertyChanged;
                RaisePropertyChanged("StartElement");
            }
        }

        public MultiTouchElementViewModel EndElement
        {
            get { return _endElement; }
            set
            {
                if(_endElement != null)
                    _endElement.PropertyChanged -= Element2PropertyChanged;
                _endElement = value;
                _endElement.PropertyChanged += Element2PropertyChanged;
                RaisePropertyChanged("EndElement");
            }
        }

        public Point StartPoint
        {
            get { return GetCenterPoint(StartElement); }
        }

        public Point EndPoint
        {
            get { return GetCenterPoint(EndElement); }
        }

        #endregion

        #region Private methods

        private Point GetCenterPoint(MultiTouchElementViewModel e)
        {
            return e.Position;
            var m = new Matrix();
            m.Translate(e.Width / 2, e.Height / 2);
            m.ScaleAt(e.ScaleX, e.ScaleY, e.Position.X, e.Position.Y);
            m.RotateAt(e.Rotation, e.Position.X, e.Position.Y);

            return m.Transform(e.Position);
        }
        
        #endregion

        #region Event handlers

        void Element1PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            RaisePropertyChanged("StartPoint");
        }

        void Element2PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            RaisePropertyChanged("EndPoint");
        }

        #endregion

    }
}
